package com.astrocsr.action.auditlog;

import java.io.InputStream;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.astrocsr.action.BaseAction;
import com.astrocsr.business.AuditLogManager;
import com.astrocsr.common.Constants;
import com.astrocsr.common.helper.ContextHelper;
import com.astrocsr.common.util.DateUtil;
import com.astrocsr.domain.db.PagingInfo;
import com.astrocsr.domain.db.SearchResult;
import com.astrocsr.domain.model.AuditLogModel;
import com.astrocsr.domain.web.CSRLogInfo;
import com.opensymphony.xwork2.ModelDriven;

@Controller("auditLogAction")
@Scope("prototype")
public class AuditLogAction extends BaseAction implements
		ModelDriven<AuditLogModel> {
 
	private static final long serialVersionUID = 1L;
	private AuditLogModel model = new AuditLogModel();  
	private String reportFile;
	private InputStream inputStream;
	
	static final Logger log = Logger.getLogger(AuditLogAction.class);
	
	private @Autowired
	AuditLogManager auditLogManager; 

	public String listAuditLog() throws Exception {
		log.info("listAuditLog.start");
		try {

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			throw ex;
		}
		log.info("listAuditLog.end");
		return SUCCESS;
	}

	public String executeSearchAuditLog() throws Exception {
		log.info("executeSearchAuditLog.start");
		try {
			
			// get listAccessrole from bussiness layer
			PagingInfo pagingInfo = model.getPagingInfo();
			if (pagingInfo == null) {
				pagingInfo = new PagingInfo();
			}
			
			String startDate = model.getStartDate();
			String endDate   = model.getEndDate();
			Date sDate = null;
			if (StringUtils.isNotBlank(startDate)) {
				sDate = DateUtil.createDateLocalSlash(startDate);
				sDate = DateUtil.resetTime(sDate, 0, 0, 0);
			}
			Date eDate = null;
			if (StringUtils.isNotBlank(endDate)) {
				eDate = DateUtil.createDateLocalSlash(endDate);
				eDate = DateUtil.resetTime(eDate, 23, 59, 59);
			}
			
			
			SearchResult<CSRLogInfo> csrLogInfoList = auditLogManager.listAuditLog(ContextHelper.getCurrentRole(),
																				   sDate,
																				   eDate,
																				   model.getSearchuid(),
																				   model.getActivityType(),
																				   pagingInfo); 
			if(csrLogInfoList != null && csrLogInfoList.size() > 0){
				model.setCsrLogInfoList(csrLogInfoList); 
				model.setPagingInfo(csrLogInfoList.getPagingInfo());
			}
			
			// insert log
			auditLogManager.addAuditlog(
					Constants.LOG_ACCT_TYPE_ACTIVITY,	//0
					Constants.LOG_ActCode_Auditlog_SER, 	//1 
					Constants.LOG_ActName_Auditlog_SER, 	//2
					ContextHelper.getLoginId(), 		//3
					ContextHelper.getCurrentRole(), 	//4
					"Search Auditlog", //5
					request.getRemoteAddr()				//6
					);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			throw ex;
		}
		log.info("executeSearchAuditLog.end");
		return SUCCESS;
	}  

	@Override
	public AuditLogModel getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	public String getReportFile() {
		return reportFile;
	}

	public void setReportFile(String reportFile) {
		this.reportFile = reportFile;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
}
